
package com.mes.system.manager;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.data.entity.EntityUtils;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.IdentityGenerator;
import com.mes.system.dao.iface.IRoleDao;
import com.mes.system.domain.Role;
import com.mes.system.manager.iface.IRoleManager;

@Component
public class RoleManager extends BasicManager<Role> implements IRoleManager {

	@Autowired
	private IRoleDao roleDao;
	
	/**
	 * 保存角色信息
	 */
	@Override
	public void saveSRole(Map<String, Collection<Role>> dataItems) throws Exception {
		for (String key : dataItems.keySet()) {
			Collection<Role> roles = dataItems.get(key);

			for (Role role : roles) {
				com.bstek.bdf2.core.model.Role bdfRole = new com.bstek.bdf2.core.model.Role();
				
				switch (EntityUtils.getState(role)) {
				case MODIFIED:
					bdfRole = roleDao.getBdfRoleByName(role.getUniqueId());
					if (bdfRole != null){
						updateBdfRole(bdfRole, role);
					}
					
					roleDao.updateBDFRole(bdfRole);
					roleDao.update(role);

					break;
				case DELETED:
					bdfRole = roleDao.getBdfRoleByName(role.getUniqueId());
					if (bdfRole != null){
						updateBdfRole(bdfRole, role);
					}
					
					roleDao.deleteBDFRole(bdfRole);
					roleDao.delete(role);
					
					break;
				case NEW:
					bdfRole.setId(IdentityGenerator.GetIdentityUUID());
					updateBdfRole(bdfRole, role);

					roleDao.saveBDFRole(bdfRole);
					roleDao.add(role);
					break;
				default:
					break;
				}
			}
		}
		
	}
	
	/**
	 * 将role的数据更新到bdfrole
	 * @param bdfRole
	 * @param role
	 */
	public static void updateBdfRole(com.bstek.bdf2.core.model.Role bdfRole, Role role){

		bdfRole.setCompanyId("111");
		bdfRole.setCreateDate(new Date());
		bdfRole.setType("normal");
		bdfRole.setName(role.getUniqueId());
		bdfRole.setDesc(role.getRoleName());
	}

}
